package algebra;

import java.text.DecimalFormat;
import java.util.Random;

public class Problem {
	private String template;
	private String problem;
	double solution;
	Fraction fSolution = null;
	double[] roots = null;
	private String slopeInterceptForm;
	private String generalForm;
	private double yIntercept;
	private double slope;
	int id;
	int equLineId;
	
	public Problem(String t){
		template = t;
	}
	
	public void setProblem(String p){
		problem = p;
	}
	
	public void setSolution(double s){
		solution = s;
		id = 0;
	}
	
	public void setSolution(Fraction f){
		fSolution = f;
		solution = f.value();
	}
	
	public void setSolution(double[] r){
		roots = r;
		id = 2;
	}
	
	public void setSolution(String sif, String gf, double y, double s){
		Random randomGenerator = new Random();
		//equLineId = randomGenerator.nextInt(4);
		equLineId = 0;
		slopeInterceptForm = sif;
		generalForm = gf;
		yIntercept = y;
		slope = s;
		id = 1;
		String p = problem;
		if(equLineId == 0){
			problem = "Re-write "+p+" in slope-intercept";
		}else if(equLineId == 1){
			problem = "Re-write "+p+" in general form";
		}else if(equLineId == 2){
			problem = "Find the y-intercept of "+p;
		}else if(equLineId == 3){
			problem = "Find the slope of "+p;
		}
	}
	
	public String toString(){
		DecimalFormat df = new DecimalFormat("#.###");
		//return "Problem: " + problem + "\nSolution: " + fSolution + " or " + df.format(solution);
		//if(fSolution != null){
		if(id == 1){
			if(equLineId == 0){
				return "Problem: "+problem+"\nSolution: "+slopeInterceptForm;
			}else if(equLineId == 1){
				return "Problem: "+problem+"\nSolution: "+generalForm;
			}else if(equLineId == 2){
				return "Problem:"+problem+"\nSolution: "+df.format(yIntercept);
			}else if(equLineId == 3){
				return "Problem:"+problem+"\nSolution: "+df.format(slope);
			}else{
				return "Problem: "+problem+"\nSlope-Intercept Form: "+slopeInterceptForm+"\nGeneral Form: "+generalForm
					+"\nY-Intercept: "+df.format(yIntercept)+"\nSlope: "+df.format(slope);
			}
		//}else if(roots != null){
		}else if(id == 2){
			return "Problem: Find the roots of " + problem + "\nx = " + df.format(roots[0]) + ", " + df.format(roots[1]);
		}else if(id == 0){
			return "Solve for x: " + problem + "\nSolution: x = " + df.format(solution);
		}else{
			System.out.println("Not a valid problem id");
			return null;
		}
	}
	
	public String getProblem(){
		return problem;
	}
	
	public String getTemplate(){
		return template;
	}
	
	public double[] getQuadSolution(){
		return roots;
	}
	
	public double getNumSolution(){
		return solution;
	}
	
	public String getSlopeForm(){
		return slopeInterceptForm;
	}
	
	public String getGeneralForm(){
		return generalForm;
	}
	
	public double getYInt(){
		return yIntercept;
	}
	
	public double getSlope(){
		return slope;
	}
}
